import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

 class User{
    private String username;
    private String password;

     public String getUsername() {
         return username;
     }

     public void setUsername(String username) {
         this.username = username;
     }

     public String getPassword() {
         return password;
     }

     public void setPassword(String password) {
         this.password = password;
     }
 }
@WebServlet("/json")
public class JsonServlet extends HttpServlet {
    //处理请求body中的json格式，最主要的是这个ObjectMapper类
    // 通过这个对象, 就可以把 json 字符串解析成 java 对象; 也可以把一个 java 对象转成一个 json 格式字符串.
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    // 通过 post 请求的 body 传递过来一个 json 格式的字符串.
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user =objectMapper.readValue(req.getInputStream(),User.class);

        //System.out.println("username :"+user.username+"  "+"password :"+user.password);
        resp.getWriter().write("okkk");
    }
}
